import AppConst from "../AppConst";
import mi from "../MI";
import MiUtil from "../util/MiUtil";


export default class LayerMgr {
    // 显示节点map
    private static _layers: { [key: number]: cc.Node; } = {};
    // 显示层次
    private static _zIndex: number = 0;


    /**
     * 初始化显示节点
     */
    public static init(): void {
        for (const name in AppConst.viewLayer) {
            const value: number = AppConst.viewLayer[name];

            const layer: cc.Node = this.createLayer(name, value);
            mi.UIRoot.addChild(layer);
            MiUtil.setWidget(layer);
            this._layers[value] = layer;
        }
    }

    /**
     * 为节点设置父节点
     * @param go 节点
     * @param layer 父节点key
     */
    public static setLayer(go: cc.Node, layer: number): void {
        const parent: cc.Node = this._layers[layer];
        go.parent = parent;
        go.zIndex = this._zIndex;
        this._zIndex++;
    }


    /**
     * 创建显示节点
     * @param name 
     * @param zIndex 
     */
    private static createLayer(name: string, zIndex: number): cc.Node {
        const layer: cc.Node = new cc.Node(name);
        layer.zIndex = zIndex;
        // 添加对齐组件, 不需要设置大小 
        // layer.setContentSize(mi.uiRoot.getContentSize());
        return layer;
    }
}
